package com.douqu.game.core.entity.sprite;

import com.douqu.game.core.entity.GameObject;
import com.douqu.game.core.entity.Asset;
import com.douqu.game.core.entity.common.CommonData;
import com.douqu.game.core.entity.goods.GoodsProp;
import com.douqu.game.core.factory.ConstantFactory;
import com.douqu.game.core.factory.DataFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by bean on 2017/7/21.
 */
public class CardLv extends GameObject {

    ///////////START 以下三个属性只是用来加载配置文件，已经转换成cardQuality对象，逻辑中调用
    public String fc;

    public String needAssets;

    public String needProps;
    /////////////////////////////////////////////END

    /**
     * 兵种升级需求
     */
    public Map<Integer, CardQuality> cardQualityMap;

    public CardLv()
    {
        cardQualityMap = new HashMap<>();
        cardQualityMap.put(ConstantFactory.QUALITY_WHITE, new CardQuality(ConstantFactory.QUALITY_WHITE));
        cardQualityMap.put(ConstantFactory.QUALITY_BLUE, new CardQuality(ConstantFactory.QUALITY_BLUE));
        cardQualityMap.put(ConstantFactory.QUALITY_PURPLE, new CardQuality(ConstantFactory.QUALITY_PURPLE));
        cardQualityMap.put(ConstantFactory.QUALITY_ORANGE, new CardQuality(ConstantFactory.QUALITY_ORANGE));
    }

    /**
     * 根据品质获取升级的一些数据
     * @param quality
     * @return
     */
    public CardQuality getByQuality(int quality) {

        return cardQualityMap.get(quality);
    }


    public void setVariable(String key, String value)
    {
        if("fc".equals(key))
        {
            this.fc = value;
            if(!"0".equals(value))
            {
                String[] strs = value.split(ConstantFactory.SEMICOLON);
                for(int i = 0; i < strs.length; i++)
                {
                    cardQualityMap.get(i+1).fc = Integer.parseInt(strs[i]);
                }
            }
        }
        else if("needAssets".equals(key))
        {
            this.needAssets = value;
            if(!"0".equals(value))
            {
                String[] strs = value.split(ConstantFactory.DIVISION);
                CommonData data = null;
                for (int i = 0; i < strs.length; i++) {
                    data = new CommonData(strs[i]);
                    Asset asset = DataFactory.getInstance().getGameObject(DataFactory.ASSET_KEY, data.id);
                    if(asset == null)
                    {
                        System.out.println("Load CardLv Error: Asset is null! id=" + data.id);
                    }
                    cardQualityMap.get(i + 1).needAsset = data;
                }
            }
        }
        else if("needProps".equals(key))
        {
            this.needProps = value;
            if(!"0".equals(value))
            {
                String[] strs = value.split(ConstantFactory.SEMICOLON);
                for (int i = 0; i < strs.length; i++)
                {
                    cardQualityMap.get(i + 1).needPropCount = Integer.parseInt(strs[i]);
                }
            }
        }
        else
            super.setVariable(key, value);
    }

    @Override
    public String toString() {
        return "cardLv{" +
                "fc='" + fc + '\'' +
                ", needAssets='" + needAssets + '\'' +
                ", needProps='" + needProps + '\'' +
                ", cardQualityMap=" + cardQualityMap +
                "} " + super.toString();
    }
}
